What is default-gateway?
The default-gateway npm package provides a simple and efficient way to find the default gateway of the local machine's network interfaces. It supports multiple operating systems, including Windows, macOS, and Linux, making it a versatile tool for network-related programming tasks. This package can be particularly useful for applications that need to interact with the network layer of the operating system, such as determining the best route for network traffic or configuring network settings programmatically.
What are default-gateway's main functionalities?
Finding the default gateway for IPv4
This feature allows you to asynchronously find the default IPv4 gateway of the local machine. The result is an object containing the gateway's IP address and the associated network interface.
const defaultGateway = require('default-gateway');
defaultGateway.v4().then(result => console.log(result));
Finding the default gateway for IPv6
Similar to the IPv4 feature, this allows for the asynchronous discovery of the default IPv6 gateway. The result includes the IPv6 gateway address and the network interface it is associated with.
const defaultGateway = require('default-gateway');
defaultGateway.v6().then(result => console.log(result));
Other packages similar to default-gateway
ip
The 'ip' package provides utilities for handling IP addresses, including the ability to parse and format them. While it doesn't offer direct functionality to find the default gateway, it complements 'default-gateway' by providing tools to work with the IP addresses that 'default-gateway' might return.
network
The 'network' package offers a broader range of network-related functionalities, including getting the user's IP address, gateway, and active network interfaces. It serves a similar purpose to 'default-gateway' but with additional features that might be useful for more comprehensive network programming tasks.
os
While not a third-party package but a core Node.js module, 'os' provides basic operating system-related utility functions. It includes methods to retrieve network interface details. However, it does not directly provide the default gateway information, making 'default-gateway' a necessary complement for specific gateway-related tasks.
default-gateway
Obtains the machine's default gateway through exec
calls to OS routing interfaces.
- On Linux and Android, the
ip
command must be available (usually provided by the iproute2
package). - On Windows,
wmic
must be available. - On IBM i, the
db2util
command must be available (provided by the db2util
package). - On Unix (and macOS), the
netstat
command must be available.
Installation
$ npm i default-gateway
Example
const defaultGateway = require('default-gateway');
const {gateway, interface} = await defaultGateway.v4();
const {gateway, interface} = await defaultGateway.v6();
const {gateway, interface} = defaultGateway.v4.sync();
const {gateway, interface} = defaultGateway.v6.sync();
API
defaultGateway.v4()
defaultGateway.v6()
defaultGateway.v4.sync()
defaultGateway.v6.sync()
Returns: result
Object
gateway
: The IP address of the default gateway.interface
: The name of the interface. On Windows, this is the network adapter name.
The .v{4,6}()
methods return a Promise while the .v{4,6}.sync()
variants will return the result synchronously.
The gateway
property will always be defined on success, while interface
can be null
if it cannot be determined. All methods reject/throw on unexpected conditions.
License
© silverwind, distributed under BSD licence